package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.RegionQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService{
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public void saveOrUpdate(Region region) {
        if(region.getId() == null){
            region.setState(Region.STATE_NORMAL);
            regionMapper.insert(region);
        }else {
            regionMapper.updateByPrimaryKey(region);
        }
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Region> list = regionMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Region> getRegionByParentId(QueryObject qo) {
        return regionMapper.selectRegionByParentId(qo);
    }

    @Override
    public void changeState(Long id, Integer state) {
        regionMapper.changeState(id,state);
    }

    @Override
    public List<Region> list() {
        return regionMapper.selectAll();
    }

    @Override
    public List<Region> queryForState(RegionQueryObject qo) {

        return regionMapper.queryForState(qo);
    }

    @Override
    public List<Region> queryAllRegions() {
        List<Region> regions=regionMapper.selectAll();
        return regions;
    }


}
